<% this.title = '首页'; %>
<% include header %>
<div class="padding">
  <div class="ui three column grid">
    <div class="eleven wide column">
      <h4 class="ui top attached block header"><i class="ui info icon"></i>公告</h4>
      <div class="ui bottom attached segment">
        <table class="ui very basic table">
          <thead>
            <tr>
              <th>标题</th>
              <th>时间</th>
            </tr>
          </thead>
          <tbody>
            <% for (let item of notices) { %>
                <tr>
                    <td><a href="<%= item.url %>"><%= item.title %></a></td>
                    <td><%= item.date %></td>
                </tr>
            <% } %>
          </tbody>
        </table>
      </div>
      <h4 class="ui top attached block header"><i class="ui signal icon"></i>排名</h4>
      <div class="ui bottom attached segment">
        <table class="ui very basic center aligned table" style="table-layout: fixed; ">
          <thead>
            <tr>
	            <th style="width: 50px; ">#</th>
	            <th style="width: 170px; ">用户名</th>
	            <th>个性签名</th>
            </tr>
          </thead>
          <script data-cfasync="false">
          var lineHeight = 0;
          (function () {
            var div = document.createElement('div');
            div.style.position = 'fixed';
            div.style.left = -10000;
            div.style.visibility = 'hidden';
            div.innerText = '测试，Test.';
            document.body.appendChild(div);
            lineHeight = div.clientHeight;
          })();
          </script>
          <tbody>
            <%
            let i = 0;
            for (let user of ranklist) {
              ++i;
            %>
                <tr>
                    <td><b><%= i %></b></td>
                    <td><a href="<%= syzoj.utils.makeUrl(['user', user.id]) %>"><%= user.username %></a><% if (user.nameplate) { %><%- user.nameplate %><% } %></td>
                    <td style="font-content">
                      <script id="user-infomation-script-<%= i %>" data-cfasync="false">
                      (function () {
                        var html = <%- serializejs(user.information) %>;
                        var elem = document.createElement('div');
                        elem.style = 'overflow: hidden; width: 100%; position: relative; ';
                        elem.style.maxHeight = lineHeight + 'px';
                        elem.innerHTML = html;
                        var imgs = Array.prototype.slice.call(elem.getElementsByTagName('img'));
                        for (var i in imgs) imgs[i].parentNode.removeChild(imgs[i]);
                        var script = document.getElementById('user-infomation-script-<%= i %>');
                        script.parentNode.replaceChild(elem, script);
                      })();
                      </script>
                    </td>
                </tr>
             <%
            } %>
          </tbody>
        </table>
      </div>
    </div>
    <div class="right floated five wide column">
        <h4 class="ui top attached block header"><i class="ui quote left icon"></i>一言（ヒトコト）</h4>
        <div class="ui bottom attached center aligned segment">
          <div class="ui active centered inline loader" id="hitokoto-loader"></div>
          <script>
          $.get('https://v1.hitokoto.cn/?c=a', function (data) {
            if (typeof data === 'string') data = JSON.parse(data);
            $('#hitokoto-loader').removeClass('active');
            $('#hitokoto-content').css('display', '').text(data.hitokoto);
            if (data.from) {
              $('#hitokoto-from').css('display', '').text('——' + data.from);
            }
          });
          </script>
          <div style="font-size: 1em; line-height: 1.5em; display: none; " id="hitokoto-content"></div>
          <div style="text-align: right; margin-top: 15px; font-size: 0.9em; color: #666; display: none; " id="hitokoto-from"></div>
        </div>
      <h4 class="ui top attached block header"><i class="ui rss icon"></i>最近更新</h4>
      <div class="ui bottom attached segment">
	<table class="ui very basic center aligned table">
          <thead>
            <tr>
              <th width="70%">题目</th>
              <th width="30%">更新时间</th>
            </tr>
	  </thead>
	  <tbody>
	    <%
	    for (let problem of problems) {
	    %>
	    <tr>
	      <td><a href="<%= syzoj.utils.makeUrl(['problem', problem.id]) %>"><%= problem.title %></a></td>
	      <td><%= problem.time %></td>
	    </tr>
	    <% } %>
	  </tbody>
	</table>
      </div>
        <%
        if (fortune) {
            let color;
            if (fortune.fortune.indexOf('吉') != -1) color = '#0ccf00';
            else if (fortune.fortune.indexOf('凶') != -1) color = '#f25e65';
            else color = '#444';
        %>
        <h4 class="ui top attached block header"><i class="ui magic icon"></i>今日运势</h4>
        <div class="ui bottom attached segment">
            <div style="height: 15px; "></div>
            <div class="ui two column center aligned padded grid">
                <div class="one column row">
                    <div style="text-align: center; ">
                        <div style="color: <%= color %>; font-size: 49px;"><%- (user.sex == -1 ? '♀ ' : '♂ ') + fortune.fortune + (user.sex == 1 ? ' <span style="transform: scaleX(-1); display: inline-block; ">♂</span>' : ' ♀') %></div>
                    </div>
                </div>
                <div class="two column row">
                    <div class="column">
                        <div style="color: #0ccf00; ">
                            <% if (fortune.good.length) { %>
                            <strong>宜：</strong><%= fortune.good[0].title %>
                            <br>
                            <span style="color: #888; font-size: 0.7em; "><%= fortune.good[0].detail %></span>
                            <div style="margin-top: 10px; "></div>
                            <strong>宜：</strong><%= fortune.good[1].title %>
                            <br>
                            <span style="color: #888; font-size: 0.7em; "><%= fortune.good[1].detail %></span>
                            <% } else { %>
                            <strong>诸事不宜<br>
                            </strong>
                            <% } %>
                        </div>
                    </div>
                    <div class="column">
                        <div style="color: #f25e65; ">
                            <% if (fortune.bad.length) { %>
                            <strong>忌：</strong><%= fortune.bad[0].title %>
                            <br>
                            <span style="color: #888; font-size: 0.7em; "><%= fortune.bad[0].detail %></span>
                            <div style="margin-top: 10px; "></div>
                            <strong>忌：</strong><%= fortune.bad[1].title %>
                            <br>
                            <span style="color: #888; font-size: 0.7em; "><%= fortune.bad[1].detail %></span>
                            <% } else { %>
                            <strong>万事皆宜<br>
                            </strong>
                            <% } %>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <% } %>
      <h4 class="ui top attached block header"><i class="ui search icon"></i>搜索题目</h4>
      <div class="ui bottom attached segment">
        <form action="<%= syzoj.utils.makeUrl(['problems', 'search']) %>" method="get">
          <div class="ui search" style="width: 100%; ">
            <div class="ui left icon input" style="width: 100%; ">
              <input class="prompt" style="width: 100%; " type="text" placeholder="ID / 题目名 …" name="keyword">
              <i class="search icon"></i>
            </div>
            <div class="results" style="width: 100%; "></div>
          </div>
        </form>
      </div>
      <h4 class="ui top attached block header"><i class="ui calendar icon"></i>近期比赛</h4>
      <div class="ui bottom attached <% if (!contests || !contests.length) { %>center aligned <% } %>segment">
        <% if (!contests || !contests.length) { %>
        还没有举行过任何比赛
        <% } else { %>
        <table class="ui very basic center aligned table">
          <thead>
            <tr>
              <th>比赛名称</th>
              <th>开始时间</th>
            </tr>
          </thead>
          <tbody>
            <%
            for (let contest of contests) {
              let now = syzoj.utils.getCurrentDate();
              let tag = '';
            %>
            <tr>
              <% if (now < contest.start_time) { %>
                <% tag = '<span class="ui header"><div class="ui mini red label">未开始</div></span>' %>
              <% } else if (now >= contest.start_time && now < contest.end_time) { %>
                <% tag = '<span class="ui header"><div class="ui mini green label">进行中</div></span>' %>
              <% } else { %>
                <% tag = '<span class="ui header"><div class="ui mini grey label">已结束</div></span>' %>
              <% } %>
              <td><a href="<%= syzoj.utils.makeUrl(['contest', contest.id]) %>"><%= contest.title %> <%- tag %></a></td>
              <td><%= syzoj.utils.formatDate(contest.start_time) %></td>
            </tr>
            <% } %>
          </tbody>
        </table>
        <% } %>
      </div>
      <% if (typeof links !== 'undefined' && links && links.length) { %>
      <h4 class="ui top attached block header font-content"><i class="ui linkify icon"></i>友情链接</h4>
      <div class="ui bottom attached segment">
        <ul style="margin: 0; padding-left: 20px; ">
          <% for (let link of links) { %>
            <li><a href="<%= link.url %>"><%= link.title %></a></li>
          <% } %>
        </ul>
      </div>
      <% } %>
    </div>
  </div>
</div>
<script>
$(function () {
  $('.ui.search').search({
    debug: true,
    apiSettings: {
      url: '/api/v2/search/problems/{query}',
      cache: false
    },
    fields: {
      title: 'name'
    }
  });
});
</script>
<% include footer %>
